package com.ruoyi.project.module.apiview.controller;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.module.apiview.model.ApiView;
import com.ruoyi.project.module.connector.domain.Connector;
import com.ruoyi.project.module.connector.service.IConnectorService;
import com.ruoyi.project.module.domain.domain.Domain;
import com.ruoyi.project.module.errorcode.domain.Errorcode;
import com.ruoyi.project.module.errorcode.service.IErrorcodeService;
import com.ruoyi.project.module.project.domain.Project;
import com.ruoyi.project.module.project.service.IProjectService;
import com.ruoyi.project.module.utils.ProjectUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/module/apiview")
public class ApiviewController extends BaseController {

    private String prefix = "module/apiview";

    @Autowired
    private IConnectorService connectorService;

    @Autowired
    private IErrorcodeService errorcodeService;

    @Autowired
    private IProjectService projectService;

    @RequiresPermissions("module:apiview:view")
    @GetMapping()
    public String apiview()
    {
        return prefix + "/apiview";
    }

    @RequiresPermissions("module:apiview:view")
    @GetMapping("/uniqueid/{uniqueid}")
    public String apiview(@PathVariable("uniqueid") Integer uniqueid, ModelMap mmap)
    {
        mmap.put("uniqueid",uniqueid);
        return prefix + "/apiview";
    }


    /**
     * 查询错误代码列表
     */
    @RequiresPermissions("module:errorcode:list")
    @PostMapping("/findByProjectnameId/{projectId}")
    @ResponseBody
    public Connector findByProjectnameId(@PathVariable("projectId") Integer projectId) throws Exception {
        Connector connector=new Connector();
        connector.setApiProjectnameId(projectId);
        startPage();
        Project project = projectService.selectProjectById(projectId);

        List<Connector> list = connectorService.selectConnectorList(connector);
        ApiView apiView ;
        if(list==null||list.isEmpty()){
            apiView = new ApiView();
        }else{
            Connector obj = list.get(0);
            apiView = new ApiView(obj);
            Domain domain = ProjectUtils.getDomainByProject(projectService, project);

            if(domain!=null){
                apiView.setDomain(domain);
                String domainUrl;
                if(domain.getPort()==80){
                    domainUrl=domain.getNamed();
                }else{
                    domainUrl=domain.getNamed()+":"+domain.getPort();
                }
                if(apiView.getUrlContextpath()!=null){
                    if(!apiView.getUrlContextpath().startsWith("/")){
                        domainUrl+="/";
                    }
                    domainUrl+=apiView.getUrlContextpath();
                }
                if(!apiView.getInterfacename().startsWith("/")){
                    domainUrl+="/";
                }
                domainUrl+=apiView.getInterfacename();
                apiView.setDomainUrl(domainUrl);
            }
        }

        Integer errorGroup = ProjectUtils.getErrorGroupByProject(projectService, project);
        apiView.setErrorGroup(errorGroup);

        return apiView;
    }

}
